package com.gdou.crrp.repository;

import com.gdou.crrp.entity.Evaluate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


public interface EvaluateRepository extends JpaRepository<Evaluate, Integer>, JpaSpecificationExecutor<Evaluate> {


    Page<Evaluate> findAllByUid(int uid,Pageable pageable);

    Evaluate findById(int id);
    @Transactional
    void deleteEvaluateById(int id);

    @Query(nativeQuery =true,value = "select level from tb_evaluate where doctor_name = ?1")
    List<Integer> findLevel(String doctorName);

    List<Evaluate> findAllByDoctorName(String doctorName);


@Query( nativeQuery =true, value = "select e.id,e.evaluate_time,e.content,e.is_eval_twice,e.level,e.e_twice_time,e.add_content,a.name from tb_evaluate e,tb_user_info a where e.doctor_name=?1 and e.uid =a.id order by e.evaluate_time desc")
      List<Object> findList(String doctor_name);

//统计数据(中评处理)
@Query( nativeQuery =true, value = " select e.id,e.evaluate_time,e.content,e.is_eval_twice,e.level,e.e_twice_time,e.add_content,a.name from tb_evaluate e , tb_user_info a where e.doctor_name=?1 and e.uid =a.id and e.level between 2 and 4 order by e.evaluate_time desc")
   List<Object> findAllByDoctorThree(String doctor_name);
//好评差评处理
    @Query( nativeQuery =true, value = " select e.id,e.evaluate_time,e.content,e.is_eval_twice,e.level,e.e_twice_time,e.add_content,a.name from tb_evaluate e , tb_user_info a where e.doctor_name=?1 and e.uid =a.id and e.level=?2 order by e.evaluate_time desc")
    List<Object> findAllByDoctorFiveOne(String doctor_name,int level);
//查找不为空的评价
    @Query( nativeQuery =true, value = " select e.id,e.evaluate_time,e.content,e.is_eval_twice,e.level,e.e_twice_time,e.add_content,a.name from tb_evaluate e , tb_user_info a where e.doctor_name=?1 and e.uid =a.id and e.add_content is not null order by e.evaluate_time desc")
    List<Object> findEvalAddContentNoNull(String doctor_name);


    List<Evaluate> findAllByDoctorNameAndLevel(String doctorName,int level);
    List<Evaluate> findAllByDoctorNameAndLevelBetween(String doctorName,int a,int b);


   List<Evaluate> findAllByDoctorNameAndAddContentIsNotNull(String doctorName);
}















//select * from table where is not null;
//@RepositoryRestResource(path = "vote")
//public interface ArticleVoteRepository extends JpaRepository<ArticleVote, Integer> {
//
//    @Query(nativeQuery = true, value = "select DISTINCT v.id,v.title,v.count,case when vu.user_id is null then 'false' else 'true' end as flag from table1 v left join table2 vu on v.id = vu.vote_id and vu.user_id=:user order by v.id desc")
//    public List<Object> findVoteList(@Param("user") Integer user);
//
//}

//@Controller
//@RequestMapping(path = "api")
//public class MainController {
//    @Autowired
//    private ArticleVoteRepository voteRepository;    @RequestMapping(path = "/getVoteList", method = RequestMethod.GET)
//    public @ResponseBody List<Object> getVoteList(@RequestParam Integer user) {
//        ArticleVoteTreeUtil voteUtil = new ArticleVoteTreeUtil();
//        return voteUtil.voteListTest(voteRepository, user);
//    }
//}
